package com.mooral.web.action.voucher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mooral.bean.Bean;
import com.mooral.bean.Vouchers;
import com.mooral.context.Context;
import com.mooral.web.action.MooralAction;

public class SearchHotTodayAction extends MooralAction {

 	public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, Context context) throws Exception {
		  
		int pageSelected = 1;
		if(request.getParameter("pageSelected")!= null){
			try{
				pageSelected = Integer.parseInt( request.getParameter("pageSelected") );
			}catch(NumberFormatException e){
				pageSelected = new Integer(1);
			}
		}		  
		  
		Vouchers vouchers = new Vouchers();
		Bean bean = new Bean();

		String sql = "SELECT * FROM vouchers WHERE expire >= NOW() ORDER BY access_day DESC limit " + (pageSelected - 1) * Context.PAGE_SIZE + "," +Context.PAGE_SIZE;
		
		vouchers.populate(bean, sql);
		
		request.setAttribute("vouchers", vouchers);
		  
		
		// Paginacion
		request.setAttribute("listSize", vouchers.getCount(sql));
		request.setAttribute("actionSearch", mapping.getPath());
		request.setAttribute("pageSelected", new Integer(pageSelected));
		//request.setAttribute("query", "");  
		return mapping.findForward(ACTION_SUCCESS);
	}
 	
}
